package com.lab.web.controller.common;

import com.lab.common.core.domain.AjaxResult;
import com.lab.common.utils.SendMail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.security.GeneralSecurityException;
import java.util.Random;


/**
 * 邮箱验证码
 */
@RestController
@RequestMapping("/email")
public class EmailController {

    @Autowired
    private SendMail emailUtil;

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    @GetMapping("/sendCode")
    public AjaxResult mail(String email) throws GeneralSecurityException {
        //生成随机数
        String captcha = String.valueOf(new Random().nextInt(899999) + 100000);
        //发生邮件
        emailUtil.sendMail(email, "您的验证码为" + captcha + "(5分钟内有效)");
        System.out.println("验证码：" + captcha);
        //将验证码保存到Redis中
        redisTemplate.opsForValue().set(email, captcha);
        redisTemplate.expire(email, 5 * 60, java.util.concurrent.TimeUnit.SECONDS);
        //响应信息
        return AjaxResult.success();
    }


}
